package com.touchapk.javafx;

import java.util.Map;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;

import com.touchapk.core.Events;



/**
 * @description
 * @author yangkeyun
 * @email coyun@qq.com
 * @date 2016年8月7日
 * @version V1.0
 */
public class SelectDeviceController implements DialogController {

	private MainController mainController;

	public SelectDeviceController() {
	}

	public SelectDeviceController(MainController mainController) {
		this.mainController = mainController;
	}

	private FXMLDialog dialog;

	@Override
	public void setDialog(FXMLDialog dialog) {
		this.dialog = dialog;
	}

	@FXML
	private ComboBox<Item> comboBox;


	@FXML
	public void okFunc() {
		if(comboBox.getSelectionModel()==null||(comboBox.getSelectionModel()!=null&&comboBox.getSelectionModel().getSelectedItem()==null)){return;}
		
		String udid=comboBox.getSelectionModel().getSelectedItem().getId();
		if(udid==null||"".equals(udid)){return;}
		
		
		this.dialog.close();
		
		mainController.runCall(udid);
		
		
		
	}

	
	class Item{
		private String id;
		private String name;
		public Item(){}
		public Item(String id,String name){
			this.id=id;
			this.name=name;
		}
		/**
		 * @return the id
		 */
		public String getId() {
			return id;
		}
		/**
		 * @param id the id to set
		 */
		public void setId(String id) {
			this.id = id;
		}
		/**
		 * @return the name
		 */
		public String getName() {
			return name;
		}
		/**
		 * @param name the name to set
		 */
		public void setName(String name) {
			this.name = name;
		}
		/* (non-Javadoc)
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {
			return this.name;
		}
		
	}

	/**
	 * Initializes the controller class.
	 */
	@FXML
	// This method is called by the FXMLLoader when initialization is complete
	void initialize() {
		
		Map<String,String> map=Events.activeUdids;
		ObservableList<Item> list = FXCollections.observableArrayList();
		for(String k:map.keySet()){
			list.add(new Item(k,map.get(k)));
		}
		
		comboBox.setItems(list);
	}
	
	
}
